import org.json4s.Xml.toJson
import org.json4s.jackson.JsonMethods.{compact, parse}

//sc.textFile("1.xml").toDS().first

val df = spark.read.textFile("1.xml")

//:paste
implicit class XmlToJson(data: String) {
    def json(root: String) = compact {
      toJson(scala.xml.XML.loadString(data)).transformField {
        case (field,value) => (field.toLowerCase,value)
      } \ root.toLowerCase
    }
    def json = compact(parse(data))
  }

val parseUDF = udf { (data: String,xmlRoot: String) => data.json(xmlRoot.toLowerCase)}
// Ctrl + D

val json = df.withColumn("value",parseUDF($"value",lit("catalog"))).select("value").map(_.getString(0))
val bookDF = spark.read.json(json).select("book.*")

bookDF.printSchema

bookDF.show(false)
